﻿using LuDragon.Common.Extensions;
using LuDragon.Data.DataHandler;
using LuDragon.IService;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;

namespace LuDragon.Service
{
    public class BaseService<T> : IBaseService<T> where T : class
    {
        protected EFContext db = new EFContext();

        #region 读      
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public T GetModel(Expression<Func<T, bool>> where)
        {
            return db.Set<T>().Where(where).AsNoTracking().FirstOrDefault();
        }
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IEnumerable<T> GetAll(string sortName = null, string sortOrder = "ASC")
        {
            if (sortName == null)
            {
                return db.Set<T>().Where(a => true).AsNoTracking();
            }
            else
            {
                return db.Set<T>().Where(a => true).Order(sortName, sortOrder).AsNoTracking();
            }

        }

        /// <summary>
        /// 分页查询所有
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="count">总记录</param>
        /// <returns></returns>
        public IEnumerable<T> GetAllWithPage(int pageIndex, int pageSize, out int count, string sortName, string sortOrder = "ASC")
        {
            var list = db.Set<T>().Where(a => true);
            count = list.Count();
            return list.Order(sortName, sortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking();
        }
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IEnumerable<T> GetByWhere(Expression<Func<T, bool>> where, string sortName = null, string sortOrder = "ASC")
        {
            if (string.IsNullOrEmpty(sortName))
            {
                return db.Set<T>().Where(where).AsNoTracking();
            }
            return db.Set<T>().Where(where).Order(sortName, sortOrder).AsNoTracking();
        }
        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="count">总记录</param>
        /// <returns></returns>
        public IEnumerable<T> GetByWhereWithPage(Expression<Func<T, bool>> where, int pageIndex, int pageSize, out int count, string sortName, string sortOrder = "ASC")
        {
            var list = db.Set<T>().Where(where);
            count = list.Count();
            return list.Order(sortName, sortOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking();
        }
        /// <summary>
        /// SQL 查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public IEnumerable<T> GetBySql(string sql, SqlParameter[] parameters = null)
        {
            if (parameters != null)
            {
                return db.Set<T>().SqlQuery(sql, parameters);
            }
            else
            {
                return db.Set<T>().SqlQuery(sql);
            }
        }


        #endregion

        #region 写
        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Add(T entity)
        {
            db.Set<T>().Add(entity);
            db.SaveChanges();
            return entity;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Set<T>().Remove(entity);
            if (db.SaveChanges() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            var entry = db.Entry<T>(entity);
            entry.State = EntityState.Modified;
            if (db.SaveChanges() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 执行Sql 返回影响条数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int ExcuteSql(string sql, SqlParameter[] parameters = null)
        {
            if (parameters == null)
            {
                return db.Database.ExecuteSqlCommand(sql);
            }
            else
            {
                return db.Database.ExecuteSqlCommand(sql, parameters);
            }
        }
        #endregion

    }
}